package com.sz.app.oss.api.trade.bill;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.trade.bill.service.TrdBillingOrderRelService;
import com.sz.biz.trade.bill.service.TrdBillingService;
import com.sz.common.base.dto.CommonModel;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import com.sz.biz.logistics.bill.entity.BillingStatus;
import com.sz.biz.logistics.bill.entity.BillingTaskStatus;
import com.sz.biz.logistics.bill.dto.BillingUrlDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Function: 贸易账单管理 <br>
 * Author: changwentao <br>
 * Date: 2017-07-18 14:18:00
 */
@RestController
@RequestMapping("${api}/biz/financial/trd/receive")
@Api(description = " ", tags = "058-002、应收账单管理")
public class TrdBillingReceiveController extends BaseController {
    @Autowired
    private TrdBillingService billingService;
    @Autowired
    private TrdBillingOrderRelService billingOrderRelService;

    @ApiOperation(value = "获取账单下所有订单", notes = "获取账单下所有订单<br>权限：" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNo", value = "账单号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "goodsName", value = "商品名", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "platformId", value = "平台id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "bizCompanyId", value = "业务方id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/orders", method = {RequestMethod.GET})
    public ResultDto getOrdersByBillNo(@RequestParam String billNo,
                                       @RequestParam(required = false) String orderNo,
                                       @RequestParam(required = false) Integer platformId,
                                       @RequestParam(required = false) String goodsName,
                                       @RequestParam(required = false) Integer bizCompanyId,
                                       @RequestParam(required = false) String startEffectTime,
                                       @RequestParam(required = false) String endEffectTime,
                                       @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                       @RequestParam(required = false, defaultValue = "10") int pageSize,
                                       @RequestParam(required = false) String sort,
                                       @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        QueryParams params = new QueryParams(pageSize, pageIndex, "", sort, asc);
        params.put("billNo", billNo);
        params.put("orderNo", orderNo);
        params.put("platformId", platformId);
        params.put("goodsName", StringUtils.replaceSqlPattern(goodsName));
        params.put("customerId", bizCompanyId);
        params.put("startEffectTime", StringUtils.isEmpty(startEffectTime) ? null : startEffectTime);
        params.put("endEffectTime", StringUtils.isEmpty(endEffectTime) ? null : endEffectTime);
        params.put("type", 1);

        return billingOrderRelService.getOrdersByBillNo(params);
    }

    @ApiOperation(value = "所有账单状态", notes = "所有账单状态")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "isAudit", value = "待审核状态： \r\n0: 返回全部状态； \r\n1： 返回待审核以上状态",
                    defaultValue = "0", dataType = "String", paramType = "query")})
    @RequestMapping(value = "/billingStatus", method = RequestMethod.GET)
    public ResultDto getBillingStatus(@RequestParam(required = false, defaultValue = "0") String isAudit) {
        List<CommonModel> list = billingService.getBillingStatus(isAudit);
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    @ApiOperation(value = "搜索账单", notes = "搜索账单<br>权限：" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNo", value = "账单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "includeAllAudit", value = "当status为AUDITING时，返回AUDITING以上的状态", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "bizCompanyIds", value = "业务方id，多个逗号分隔", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "platformIds", value = "平台id，多个逗号分隔", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startEffectTime", value = "起始出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "endEffectTime", value = "结束出帐时间(yyyy-MM-dd HH:mm:ss)", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findBillingList(@RequestParam(required = false) String billNo,
                                     @RequestParam(required = false) String orderNo,
                                     @RequestParam(required = false) BillingStatus status,
                                     @RequestParam(required = false) boolean includeAllAudit,
                                     @RequestParam(required = false) String platformIds,
                                     @RequestParam(required = false) String bizCompanyIds,
                                     @RequestParam(required = false) String startEffectTime,
                                     @RequestParam(required = false) String endEffectTime,
                                     @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @RequestParam(required = false, defaultValue = "10") int pageSize,
                                     @RequestParam(required = false) String sort,
                                     @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        QueryParams params = new QueryParams(pageSize, pageIndex, "", sort, asc);
        params.put("startEffectTime", StringUtils.isEmpty(startEffectTime)?null:startEffectTime);
        params.put("endEffectTime", StringUtils.isEmpty(endEffectTime)?null:endEffectTime);
        params.put("billNo", billNo);
        params.put("orderNo", orderNo);
        if (!StringUtils.isEmpty(bizCompanyIds)) {
            String[] bizCompanyIdArray = bizCompanyIds.split(",");
            params.put("bizCompanyIds", bizCompanyIdArray);
        }
        if (!StringUtils.isEmpty(platformIds)) {
            String[] platformIdArray  = platformIds.split(",");
            params.put("platformIds", platformIdArray);
        }


        params.put("status", status);
        params.put("type", 1);
        params.put("includeAllAudit", includeAllAudit);
        params.put("taskStatus", BillingTaskStatus.COMPLETED);
        return billingService.findByFilter(params);
    }

    @ApiOperation(value = "根据账单号删除账单", notes = "根据账单号删除账单<br>权限：" + Permissions.BIZ_MGT_TRD_BILL_RECEIVE)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "billNos", value = "billNos", required = true, dataType = "List", paramType = "body")})
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_TRD_BILL_RECEIVE)
    public ResultDto batchDeleteBillingByNos(@RequestBody List<String> billNos) {
        billingService.batchdeleteByNos(billNos);
        return getDeleteSuccessDto(MessagesCodes.MODULE_FINANCIAL);
    }

    @ApiOperation(value = "提交审核", notes = "提交审核-权限<br>" + Permissions.BIZ_MGT_TRD_BILL_RECEIVE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNos", value = "账单号集合", dataType = "List", paramType = "body", required = true)
    })
    @RequestMapping(value = "/submit", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_BILL_RECEIVE})
    public ResultDto submit(@RequestBody List<String> billNos) throws ParseException {
        billingService.submit(billNos);
        return getSubmitSuccessDto(MessagesCodes.MODULE_FINANCIAL);
    }


    @ApiOperation(value = "审核账单,已付款", notes = "审核账单,已付款-<br>" + Permissions.BIZ_MGT_TRD_BILL_RECEIVE_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map", value = "billNos, \r\ntype(CHARGEOFF: 审核通过；DRAFT： 打回草稿； PAID： 已付款), \r\nmsg", required = true, dataType = "Map", paramType = "body")
    })
    @RequestMapping(value = "/audit", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_BILL_RECEIVE_APPROVAL})
    public ResultDto audit(@RequestBody Map<String, Object> map) throws ParseException {
        BillingStatus statusTo = billingService.audit(map);
        switch (statusTo) {
            case DRAFT:
                return getRejectSuccessDto();
            case CHARGEOFF:
                return getChargeoffSuccessDto(MessagesCodes.MODULE_FINANCIAL);
            case PAID:
                return getOperatePaidSuccessDto(MessagesCodes.MODULE_FINANCIAL);
            default:
                return getOperationSuccessDto(MessagesCodes.MODULE_FINANCIAL);
        }
    }

    @ApiOperation(value = "价格调整", notes = "价格调整-<br>只有待审核状态下可以调整价格<br>" + Permissions.BIZ_MGT_TRD_BILL_RECEIVE_APPROVAL)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billingDto", value = "billNo, \r\nadjustmentTypeCode(UPPER: 上调；DOWN： 下调), " +
                    "\r\nadjustmentValue, adjustmentUnit(1: %; 2: 金额), adjustmentRemark", required = true, dataType = "BillingDto", paramType = "body")
    })
    @RequestMapping(value = "/adjustment", method = {RequestMethod.POST})
    @RequiresPermissions(value = {Permissions.BIZ_MGT_TRD_BILL_RECEIVE_APPROVAL})
    public ResultDto adjustment(@RequestBody Map<String, String> billingDto) throws ParseException {
        billingService.adjustment(billingDto);
        return getBillingAdjustmentSuccessDto(MessagesCodes.MODULE_FINANCIAL);
    }

    /**
     * 所有价格调整类型： 上调和下调
     *
     * @return
     */
    @RequestMapping(value = "/adjustmentType", method = RequestMethod.GET)
    @ApiOperation(value = "所有价格调整类型", notes = "所有价格调整类型")
    public ResultDto getAdjustmentType() {
        List<CommonModel> list = billingService.getAdjustmentType();
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }

    /**
     * 账单导出
     */
    @ApiOperation(value = "账单导出", notes = "账单导出-权限<br>"  )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "billNos", value = "账单号集合", dataType = "List", paramType = "body", required = true)
    })
    @RequestMapping(value = "/export", method = {RequestMethod.POST})
    public ResultDto export(@RequestBody List<String> billNos) throws ParseException {
        List<BillingUrlDto> list = billingService.export(billNos);
        ResultDto result = new ResultDto();
        result.setData(list);
        return result;
    }


}
